package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mapper.AddressBookMapper;
import com.example.pojo.AddressBook;
import com.example.service.AddressBookService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements AddressBookService {
    /**
     * 设置默认地址
     * @param addressBook
     * @param userId
     */
    @Override
    @Transactional  //涉及多次表操作
    public void setDefault(AddressBook addressBook, Long userId) {
        //先将当前用户下所有地址置为非默认状态
        LambdaUpdateWrapper<AddressBook> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AddressBook::getUserId, userId);
        updateWrapper.set(AddressBook::getIsDefault, 0);
        this.update(updateWrapper);

        //再将当前地址设为默认
        addressBook.setIsDefault(1);
        this.updateById(addressBook);
    }
}
